package UPPM;

import java.util.concurrent.TimeUnit;

import org.junit.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.*;
import java.util.List;
import java.util.NoSuchElementException;
import junit.framework.Assert;
import org.junit.*;
import com.thoughtworks.selenium.webdriven.commands.IsTextPresent;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;



public class TestNewQuickSearch {
	
	WebDriver driver = new FirefoxDriver();
	
	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");	
		}
	
	@Test
	public void TestQuickSearch() throws Exception {
		
		
		Utils.waitForElement(driver, By.className("ac_input"));
		WebElement quick_search =  driver.findElement(By.className("ac_input"));
		driver.findElement(By.className("ac_input")).clear();
		quick_search.click();
		Thread.sleep(1000);
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//		quick_search.click();
		WebElement quick_search_track =  driver.findElement(By.className("ac_input"));
		quick_search_track.sendKeys("Rock");
		driver.findElement(By.id("quick_search_btn")).click();
		WebElement SearchButton = driver.findElement(By.id("quick_search_btn"));
		SearchButton.click();
		Utils.waitForElement(driver, By.xpath(".//*[@id='search-results']/div[1]/div[3]/ul/li[2]/li[1]/a"));
		
//		Verify duration filters
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement duration_FullLength = driver.findElement(By.xpath(".//*[@id='search-tree-menu']/li[2]/ul/li[1]/a[3]"));
		Assert.assertEquals("Full Length", duration_FullLength.getText().trim());
		System.out.println(duration_FullLength.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement music_for = driver.findElement(By.linkText("Music For"));
		Assert.assertEquals("Music For", music_for.getText().trim());
		System.out.println(music_for.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement music_styles = driver.findElement(By.linkText("Music Styles"));
		Assert.assertEquals("Music Styles", music_styles.getText().trim());
		System.out.println(music_styles.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement moods = driver.findElement(By.linkText("Moods"));
		Assert.assertEquals("Moods", moods.getText().trim());
		System.out.println(moods.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement instruments = driver.findElement(By.linkText("Instruments"));
		Assert.assertEquals("Instruments", instruments.getText().trim());
		System.out.println(instruments.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Utils.waitForElement(driver, By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div/div/ul/li[11]/h3"));
		WebElement tempos = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div/div/ul/li[11]/h3"));
		Assert.assertEquals("Tempos", tempos.getText().trim());
		System.out.println(tempos.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Utils.waitForElement(driver, By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div/div/ul/li[13]/h3"));
		WebElement eras = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div/div/ul/li[13]/h3"));
		Assert.assertEquals("Eras", eras.getText().trim());
		System.out.println(eras.getText());
		
			
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Utils.waitForElement(driver, By.xpath(".//*[@id='search-tree-menu']/li[2]/ul/li[7]/a[3]"));
		WebElement labels = driver.findElement(By.xpath(".//*[@id='search-tree-menu']/li[17]/h3"));
		Assert.assertEquals("Labels", labels.getText().trim());
		System.out.println(labels.getText());
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebElement search_results = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div[2]/div/div/div/span"));
		System.out.println(search_results.getText());
	
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.findElement(By.className("include-filter-item")).click();
				
		Utils.waitForElement(driver, By.xpath(".//*[@id='search-tree-menu']/li[17]/h3"));
		
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		Thread.sleep(1000);
		WebElement new_search_results = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div[3]/div[2]/div/div/div/span"));
		System.out.println(new_search_results.getText());
		
		Assert.assertNotSame(search_results.getText(), new_search_results.getText());
		
		WebElement Element = driver.findElement(By.xpath(".//*[@id='current-selections']/ul/li[5]/ul/li/span"));
		Assert.assertEquals(true, Element.isDisplayed());
		
		}

	@After
	public void tearDown() throws Exception {
		System.out.println("******************************");
		driver.quit();
	}
	
}